package com.htjc.integral.exchange.service.impl;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.naming.event.ObjectChangeListener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.htjc.integral.exchange.mapper.EcBpMemberDetailMapper;
import com.htjc.integral.exchange.service.IEcIntegralDetailService;
import com.htjc.integral.model.exchange.EcBpMemberDetail;

import lombok.extern.slf4j.Slf4j;
@Slf4j(topic="exchangeLogger")
@Service
public class EcIntegralDetailServiceImpl implements IEcIntegralDetailService {
	

	@Autowired
	private EcBpMemberDetailMapper ecBpMemberDetailMapper;
	
/*
	private static final String IS_ALLOW_QUKUAILIAN = SgccMallUtil.getInstance().getConfValue("is.allow.qukuailian.qkl");
	//区块链积分比例
	private static final String QKL_INTEGRAL_SCALE = SgccMallUtil.getInstance().getConfValue("qkl.integral.scale");
	//区块链ip
	private static final String QKL_INTEGRAL_IP = SgccMallUtil.getInstance().getConfValue("qkl.integral.ip");*/
	
	
	
	
	@Override
	public Integer getOverdueIntegral(String userId) {
		Integer tatalValueRemain = 0;
		Map<String, String> map = new HashMap<String, String>();
		map.put("memberId", userId);
		List<EcBpMemberDetail> list = ecBpMemberDetailMapper.getOverdueIntegral(map);
		if(list!=null && list.size()>0){
			for (EcBpMemberDetail ecBpMemberDetail : list) {
				tatalValueRemain += ecBpMemberDetail.getBpValueRemain();
			}
		}
		return tatalValueRemain;
	}
	
	
	
	@Override
	public Integer getAvailabelIntegralById(String userId) {
		Map<String, String> map = new HashMap<String, String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("bpIsFrozen", "0");
		map.put("bpChangeType", "0");
		map.put("currentTime", sdf.format(new Date()));
		map.put("bpValueRemain", "0");
		map.put("memberId", userId);
		int i= ecBpMemberDetailMapper.getMemberDetailBpValue(map);
		return new Integer(i);
	}
	
	
	
	
	@Override
	public Map<String, Integer> getEveryIntegralById(String userId) {
		// TODO Auto-generated method stub
		Map<String, Integer>resultMap = new HashMap<String, Integer>();
		Integer totalValue = 0;//总积分
		Integer availableValue = 0;//可用积分
		Integer canNotValue = 0;//不可用积分
		Map<String, String> map = new HashMap<String, String>();
		map.put("memberId", userId);
		//从数据库查询积分
		List<EcBpMemberDetail> list = ecBpMemberDetailMapper.getMemberDetail(map);
		for (EcBpMemberDetail ecBpMemberDetail : list) {
			if(ecBpMemberDetail.getBpChangeType()!=null && ecBpMemberDetail.getBpChangeType().equals("0")){
				totalValue += ecBpMemberDetail.getBpValueRemain();
				if(ecBpMemberDetail.getBpIsFrozen()!=null && ecBpMemberDetail.getBpIsFrozen().equals("0") 
						&& ecBpMemberDetail.getStartTime() != null && ecBpMemberDetail.getEndTime() !=null
						&& new Date().getTime()>=ecBpMemberDetail.getStartTime().getTime() 
						&& new Date().getTime() <= ecBpMemberDetail.getEndTime().getTime()){
					availableValue += ecBpMemberDetail.getBpValueRemain();
				}
			}
		}
		canNotValue = totalValue - availableValue;
		resultMap.put("totalValue", totalValue);
		resultMap.put("availabelValue", availableValue);
		resultMap.put("canNotValue", canNotValue);
		
		log.info(resultMap.toString());
		return resultMap;
	}



	
}
